View Javadoc
1   package edu.jiangxin.apktoolbox.file.password.recovery.checker;
2   
3   import com.github.junrar.Archive;
4   import com.github.junrar.exception.CrcErrorException;
5   import com.github.junrar.exception.RarException;
6   import com.github.junrar.exception.UnsupportedRarV5Exception;
7   import com.github.junrar.rarfile.FileHeader;
8   import edu.jiangxin.apktoolbox.file.password.recovery.exception.UnknownException;
9   import edu.jiangxin.apktoolbox.file.password.recovery.exception.UnsupportedVersionException;
10  
11  import java.io.IOException;
12  import java.io.OutputStream;
13  
14  public final class RarChecker extends FileChecker {
15      private static final boolean DEBUG = false;
16  
17      public RarChecker() {
18          super();
19      }
20  
21      @Override
22      public String[] getFileExtensions() {
23          return new String[]{"rar"};
24      }
25  
26      @Override
27      public String getFileDescription() {
28          return "*.rar";
29      }
30  
31      @Override
32      public String getDescription() {
33          return "RAR Checker(Not support RAR5+)";
34      }
35  
36      @Override
37      public boolean prepareChecker() {
38          return true;
39      }
40  
41      @Override
42      public boolean checkPassword(String password) {
43          boolean result = false;
44          try (Archive archive = new Archive(file, password)) {
45              while (true) {
46                  FileHeader fileHeader = archive.nextFileHeader();
47                  if (fileHeader == null) {
48                      break;
49                  }
50                  archive.extractFile(fileHeader, new OutputStream() {
51                      @Override
52                      public void write(int b) {
53                      }
54                  });
55              }
56              result = true;
57          } catch (CrcErrorException e) {
58              if (DEBUG) {
59                  logger.error("[CrcErrorException]password is incorrect: " + password);
60              }
61          } catch (UnsupportedRarV5Exception e) {
62              throw new UnsupportedVersionException(e);
63          } catch (RarException e) {
64              if (DEBUG) {
65                  logger.error("[RarException]password is incorrect: " + password);
66              }
67          } catch (IOException e) {
68              throw new UnknownException(e);
69          }
70          return result;
71      }
72  }